package com.blog.admin.feign;

import com.aliyun.oss.OSS;
import com.blog.admin.config.OssConfiguration;
import com.blog.system.api.domain.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 文件上传服务类
 * </p>
 *
 * @author Valerie
 * @date 2023/3/13
 **/
@Service
@Slf4j
public class uploadFileServiceImpl implements UploadFileService{

    @Resource
    private OSS oss;

    @Resource
    private OssConfiguration ossConfiguration;

    @Override
    public SysFile uploadFile(String filePath, String fileName, MultipartFile file) throws IOException {
        oss.putObject(ossConfiguration.getBucketName(), filePath + fileName, file.getInputStream());
        log.info("上传文件：{}", fileName);
        return SysFile.builder()
                .name(fileName)
                .url(getFilePath(filePath + fileName))
                .build();
    }

    @Override
    public Boolean exits(String filePath){
        return oss.doesObjectExist(ossConfiguration.getBucketName(), filePath);
    }

    @Override
    public String getFilePath(String filePath){
        return ossConfiguration.getUrl() + filePath;
    }

}
